class_name Api extends Node

var httpRequest = null
var requestHeaders = [
	"Content-Type: application/json", 
	"Accept: application/json"
]
const BASE_URL = "http://localhost:8081"
	
	
func _init():
	httpRequest = HTTPRequest.new()
	
	
func handleBody(body):
	var json = JSON.new()
	var parse_error = json.parse(body.get_string_from_utf8())
	return json.get_data()
		
func updateOrCreate(obj, mac, body, callback):
	obj.add_child(httpRequest)
	httpRequest.request_completed.connect(
		func(_a, _b, _c, body):
			var data = handleBody(body)
			if data.code == 200:
				callback.call(data.data)
			else:
				callback.call(null)
	)
	var url = BASE_URL + "/playerData/updateOrCreate?mac=" + mac
	httpRequest.request(url, requestHeaders, HTTPClient.METHOD_POST, JSON.stringify(body))
